define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                queryParams:function(params) {
                    // 要显示的字段
                    params.fields = '';
                    // 要关联的拓展信息
                    //params.extra = 'live';
                    return params;
                },
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'apply_id', title: __('apply_id'),align:'left'},
                        {field: 'quantity', title: __('quantity'),align:'left'},
                        {field: 'sync_count', title: __('sync_count'),align:'left'},
                        {field: 'apply_reason', title: __('apply_reason'),align:'left'},
                        {field: 'status_text', title: __('audit_status'),align:'left',formatter: Controller.api.formatter.status_text},
                        //{field: 'audit_comment', title: __('audit_comment'),align:'left'},
                        {field: 'createtime', title: __("Create time"), formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Update time'), formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'operate', title: __('Operate'), formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $(document).on('click', "#sync", function () {
                    var apply_id = $(this).data('id');
                    $.post("/wechat/shakearound/device/sync", {apply_id:apply_id}, function (ret) {
                        var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
                        if (ret.code == 1) {
                            Backend.api.toastr.success(msg);
                            setTimeout(function(){
                                location.href = ret.url;
                            },2000);
                        } else {
                            Backend.api.toastr.error(msg ? msg : __('Operation failed'));
                        }
                    }, 'json');
                });
            },
            formatter:{
                status_text: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.audit_status == 2) {
                        _color = 'success';
                    }else if (row.audit_status == 1){
                        _color = 'danger';
                    }else{
                        _color = 'primary';
                    }
                    return '<span class="text-'+ _color +'">' + row.status_text + '</span>';
                },
                operate: function (value, row, index) {
                    var _operate = '';
                    var surplus = row.quantity - row.sync_count;
                    if (surplus < 1){
                        _operate += '&nbsp;<a href="javascript:;" data-id="'+row.apply_id+'" id="sync" class="btn btn-info btn-xs disabled">' + __('sync') + '</a>&nbsp;';
                        _operate += '&nbsp;<a href="/wechat/shakearound/device/index" class="btn btn-info btn-xs">' + __('Show device') + '</a>&nbsp;';
                    }else{
                        _operate += '&nbsp;<a href="javascript:;" data-id="'+row.apply_id+'" id="sync" class="btn btn-info btn-xs">' + __('sync') + '</a>&nbsp;';
                        _operate += '&nbsp;<a href="javascript:;" class="btn btn-info btn-xs disabled">' + __('Show device') + '</a>&nbsp;';
                    }
                    return _operate;
                }
            }
        }
    }
    return Controller;
});